红酒品质数据分析

# Load the Data
winedata=read.csv('wineQualityReds.csv')
# winedata=read.csv('wineQualityWhites.csv')

第一部分 数据总览

library(ggplot2)
winedata$quality_factor=factor(winedata$quality)
winedata$acidity=with(data=winedata,fixed.acidity+volatile.acidity+citric.acid)

1. 从图中可以看出,品质为5和6的酒样本数量最多,占样本总数80%左右。

2. 从5和6中看出非挥发性酸基本上呈正态分布,其他图中由于样本数太少,不足以看出分布情况。

3. 易挥发酸性酸与挥发性酸的分布情况相似。

4. 柠檬酸

5. 剩余糖为对称分布。

6. 氯化物为对称分布。

7. 单体硫为含量越低样本数量越多,在含量为20以后样本数量下降很快。

8. 从5、6、7图可以看出总硫量分布为单调递减,而且在含量为50以后样本数量下降很快,与单体硫的分布相似,这两个变量应该具有正向相关性。

9. 密度

10. pH值

11. 硫酸盐

12. 酒精度

13. 酸总量

第二部分 总体分析

1. What is the structure of your dataset?

数据集共有1599条样本数据,11个自变量和1个目标变量,自变量为包含了红酒的化学成分以及这些成分的具体含量,目标变量为品酒师对就的质量作出的评价。

2. What is/are the main feature(s) of interest in your dataset?

我感兴趣的变量有citric.acid(柠檬酸)、free.sulfur.dioxide(游离二氧化硫)、total.sulfur.dioxide(总硫量)、alcohol(酒精度),从这些变量的的直方图可以看出,这些这些变量的分布是分布是不对称的,包含的信息较多。

3. What other features in the dataset do you think will help support your investigation into your feature(s) of interest?

密度变量与酒的品质也有一定关系

4. Did you create any new variables from existing variables in the dataset?

1.quality变量经过factor函数衍生出quality_factor变量 2.把所有酸加和的acidity字段

5. Of the features you investigated, were there any unusual distributions? Did you perform any operations on the data to tidy, adjust, or change the form of the data? If so, why did you do this?

没有

第三部分 双变量图

1. 全量矩阵图

total.sulfur.dioxide与free.sulfur.dioxide之间相关性最高,相关系数为0.668;从total.sulfure.dioxide变量中看出数据中存在离群点;

2. 所有变量与quality_factor之间的boxplot

非挥发性酸与品质之间没有很明显的关联性,3~7可以看出在均值在上升,8的均值与7相比下降,有可能是由于8的样品数量太少导致;样本分布比较分散。

3. 从图中可以看出挥发性酸与品质之间有负相关性,挥发性酸的均值随着品质提升而下降;样本分布比较集中。

4. 柠檬酸的均值虽然随着品质升高而上升,但是样本分布很分散,5~8的样本极大值相差不大。

5. 剩余糖量与品质之间的相关度低,大部分样本的糖剩余量都在2左右;样本分布比较分散。

6. 氯化物样本分布比较集中,均值和分布情况与品质之间相关度低。

7. 游离二氧化硫与品质之间呈先升高后降低的趋势。

8. 二氧化硫含量与品质之间呈先上升后下降趋势。

9. 密度与品质之间呈下降趋势,品质越高密度越低。

10. pH与品质呈负相关性,品质越高pH值越低。

11. 硫酸盐含量与品质呈正相关性,品质越高硫酸盐含量越高。

12. 酒精度与品质之间基本上呈正相关性,但品质为5的均值低于4和6,其他样本均值呈上升趋势。

第四部分 双变量分析

1. Talk about some of the relationships you observed in this part of the investigation. How did the feature(s) of interest vary with other features in the dataset?

通过观察每个变量与品质变量之间的boxplot关系发现,酒的品质越高,volatile.acidity(挥发性酸)均值越低,citric acid(柠檬酸)均值越来越高,density(密度)均值也是基本上呈下降趋势,pH值均值也呈略微下降趋势,sulphates(硫酸盐)均值呈上升趋势,alcohol(酒精度)均值上升

2. Did you observe any interesting relationships between the other features (not the main feature(s) of interest)?

变量total.sulfur.dioxide和free.sulfur.dioxide之间呈正相关性;total.sulfur.dioxide和free.sulfur.dioxide与酒的品质呈先上升后下降趋势,可能存在高次相关性。

3. What was the strongest relationship you found?

酒精度与酒的品质的相关度最大;total.sulfur.dioxide与free.sulfur.dioxide相关系数最高。

第五部分 多变量图

1. 从下图可以看出fixed.acidity与density之间相互增强,为了扩大区分度,将density变量取平方,从线性拟合图可以看出酒品质越高,线性拟合越靠下,说明这两种成分的含量可以区分酒的品质。

# fixed.acidity vs. density^2 vs. quality
ggplot(data=subset(winedata,total.sulfur.dioxide<200),
       aes(x=fixed.acidity,y=density*density,color=quality_factor))+
    geom_point(alpha = 0.9, size = 2, position = 'jitter')+
    geom_smooth(method = 'lm')+
    ggtitle('fixed.acidity vs. density vs. quality')

2. 从下图可以看出酒精度和密度之间呈下降趋势,但是线性拟合曲线相互重叠,并不能区分酒的品质。

决策树

## n= 1599 
## 
## node), split, n, loss, yval, (yprob)
##       * denotes terminal node
## 
##  1) root 1599 918 5 (0.0063 0.033 0.43 0.4 0.12 0.011)  
##    2) alcohol< 10.25 842 323 5 (0.0083 0.034 0.62 0.31 0.03 0.0024)  
##      4) sulphates< 0.575 353  91 5 (0.011 0.054 0.74 0.19 0.0057 0) *
##      5) sulphates>=0.575 489 232 5 (0.0061 0.02 0.53 0.4 0.047 0.0041)  
##       10) volatile.acidity>=0.3175 443 193 5 (0.0068 0.023 0.56 0.37 0.034 0.0023) *
##       11) volatile.acidity< 0.3175 46  16 6 (0 0 0.15 0.65 0.17 0.022) *
##    3) alcohol>=10.25 757 379 6 (0.004 0.032 0.21 0.5 0.23 0.021)  
##      6) alcohol< 11.55 507 248 6 (0.0059 0.041 0.28 0.51 0.15 0.0079)  
##       12) volatile.acidity>=0.375 375 171 6 (0.008 0.053 0.31 0.54 0.077 0.0027)  
##         24) sulphates< 0.585 123  67 5 (0.024 0.11 0.46 0.37 0.041 0) *
##         25) sulphates>=0.585 252  93 6 (0 0.024 0.25 0.63 0.095 0.004) *
##       13) volatile.acidity< 0.375 132  77 6 (0 0.0076 0.2 0.42 0.36 0.023)  
##         26) pH>=3.265 75  38 6 (0 0.013 0.27 0.49 0.23 0) *
##         27) pH< 3.265 57  27 7 (0 0 0.11 0.32 0.53 0.053) *
##      7) alcohol>=11.55 250 131 6 (0 0.012 0.072 0.48 0.39 0.048)  
##       14) sulphates< 0.685 136  53 6 (0 0.022 0.11 0.61 0.25 0.0074) *
##       15) sulphates>=0.685 114  50 7 (0 0 0.026 0.32 0.56 0.096) *

第六部分 多变量分析

1. Talk about some of the relationships you observed in this part of the investigation. Were there features that strengthened each other in terms of looking at your feature(s) of interest?

fixed.acidity与density之间相互增强;

2. Were there any interesting or surprising interactions between features?

alcohol与density之间相互减弱,从物理的角度可以解释这个原因,酒精的密度比水低,酒精含量越高酒的密度就越低。

OPTIONAL: Did you create any models with your dataset? Discuss the strengths and limitations of your model.

尝试使用决策树建模,但是结果只能区分出5,6,7三种酒,其他的品质的酒区分不明显


第七部分 最终图和总结

图一

qplot(data=winedata,x=quality)

描述一

通过对单quality单一变量的分析看出样本分布不均匀,5、6样本量总和超过总样本80%,为后续分析数据带来不利因素。

图二

ggplot(aes(x=quality_factor,y=citric.acid),data=subset(winedata,total.sulfur.dioxide<200))+
  geom_jitter(alpha=0.3)+
  geom_boxplot(alpha=0.5)+
  ggtitle('柠檬酸 vs. 品质')+
  theme(text=element_text(family="STKaiti",size=14))

描述二

通过其他变量与酒的品质变量之间的boxplot可以看出一些变量的均值与品质之间存在很强的相关性,比如citric.acid变量均值与品质之间存在正相关性,酒的品质越高citric.acid含量越高。

图三

ggplot(data=subset(winedata,total.sulfur.dioxide<200),
       aes(x=fixed.acidity,y=density*density,color=quality_factor))+
    geom_point(alpha = 0.9, size = 2, position = 'jitter')+
    geom_smooth(method = 'lm')+
    ggtitle('fixed.acidity vs. density vs. quality')

描述三

#### 通过多变量分析可以看出fixed.acidit与density之间相互增强,同时这两个变量之间的线性组合可以很好的区分出酒的品质。

第八部分 反思

1.这些酒的品质是品酒师通过嗅觉和味觉给出的判断,带有一定的主观性,可能会给目标变量带来一定的干扰;

3.此数据集中样本分布不均匀,品质为5和6的样本数量占到样本总和的80%,如果减少这两种样本,样本总数又太少;

2.酒的化学成分在一定程度上决定了酒的口感,所有这些化学成分的配比才是决定口感的更重要因素。